// store/index.js
import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    token: localStorage.getItem('token') || null,
    user: {},// 添加 user 字段
    userInfo: {}
  },
  getters: {
    isLoggedIn: state => !!state.token,
    user: state => state.user,// 添加 user 的 getter
    userInfo: state => state.userInfo
  },
  mutations: {
    SET_TOKEN(state, token) {
      state.token = token;
      localStorage.setItem('token', token);
    },
    REMOVE_TOKEN(state) {
      state.token = null;
      localStorage.removeItem('token');
    },
    SET_USER(state, user) {
      state.user = user; // 添加 SET_USER mutation
      localStorage.setItem('user', JSON.stringify(user));
    },
    SET_USER_INFO(state, userInfo) {
      state.userInfo = userInfo;
      localStorage.setItem('userInfo', JSON.stringify(userInfo));
    }
  },
  actions: {
    login({ commit }, token) {
      commit('SET_TOKEN', token);
    },
    logout({ commit }) {
      commit('REMOVE_TOKEN');
      commit('SET_USER', {}); // 登出时清空 user 信息
      commit('SET_USER_INFO', {});
      localStorage.removeItem('user');
      localStorage.removeItem('userInfo');
    },
    /*
    getUserInfo({ commit, state }) {
      if (state.token) {
        return new Promise((resolve, reject) => {
          Vue.prototype.$axios.get('/user/info')
            .then(response => {
              if (response.data.code === 200) {
                commit('SET_USER_INFO', response.data.data);
                commit('SET_USER', response.data.data); // 更新 user 信息
                resolve(response.data.data);
              } else {
                reject(response.data.message);
              }
            })
            .catch(error => {
              console.error(error);
              reject('获取用户信息失败');
            });
        });
      }
    }
    */

    // 获取用户信息
    getUser({ commit, state }) {
      if (state.token) {
        return new Promise((resolve, reject) => {
          Vue.prototype.$axios.get('/user/info')
            .then(response => {
              if (response.data.code === 200) {
                commit('SET_USER', response.data.data);
                resolve(response.data.data);
              } else {
                reject(response.data.message);
              }
            })
            .catch(error => {
              console.error('获取用户信息失败:',error);
              reject('获取用户信息失败');
            });
        });
      }
    },

    // 获取用户详细信息
    getUserInfo({ commit, state }) {
      if (state.token && state.user.id) {
        return new Promise((resolve, reject) => {
          Vue.prototype.$axios.get(`/userInfo/get/${state.user.id}`)
            .then(response => {
              if (response.data.code === 200) {
                commit('SET_USER_INFO', response.data.data);
                resolve(response.data.data);
              } else {
                reject(response.data.message);
              }
            })
            .catch(error => {
              console.error('获取用户详细信息失败:', error);
              reject('获取用户详细信息失败');
            });
        });
      } else {
        return Promise.reject('token 或 user.id 不存在');
      }
    }

  }
});